package com.haierp.task;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Set;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.haierp.service.IOuterOrderService;
import com.haierp.service.xiaohongshu.IXiaohongshuService;
import com.haierp.service.youzan.IYouzanService;

/**
 * 自动去有赞下载订单
 * 
 * @author LiuYang
 *
 */
@Component
public class AutoYouzanTradesSoldGetTask {
	protected Logger logger = LogManager.getLogger(getClass());
	
	@Autowired
	private IYouzanService youzanService;

	@Autowired
	private IXiaohongshuService xiaohongshuService;

	@Autowired
	private IOuterOrderService outerOrderService;
	
	//每隔半小时执行一次
	@Scheduled(cron = "0 0/30 * * * ?")
	public void run(){
		logger.info("定时任务：自动去有赞下载订单===>Start");
		List<Long> outOrderIdList = youzanService.youzanTradesAdd();
		if(outOrderIdList.size() > 0) {
			//生成子订单并配货
			outOrderIdList.forEach(erpOrderId -> {
				outerOrderService.review(erpOrderId,"YZ");
			});
		}
		logger.info("定时任务：自动去有赞下载订单===>End");
		
		logger.info("定时任务：自动下架已售罄的商品===>Start");
		youzanService.youzanSaleOutItemToDelisting();
		logger.info("定时任务：自动下架已售罄的商品===>End");
	}
	
	//小红书每隔半小时执行一次
	@Scheduled(cron = "0 0/45 * * * ?")
	public void runOne(){
		logger.info("定时任务：自动去小红书下载订单===>Start");
		List<Long> outOrderIdList = xiaohongshuService.xiaohongshuTradesAdd("CFK");
		if(outOrderIdList.size() > 0) {
			//生成子订单并配货
			outOrderIdList.forEach(erpOrderId -> {
				outerOrderService.review(erpOrderId,"XHS");
			});
		}
		logger.info("定时任务：自动去小红书下载订单===>End");
	}

	//小红书每隔半小时执行一次
	@Scheduled(cron = "0 0/45 * * * ?")
	public void addXHSItem() throws Exception {
		logger.info("定时任务：自动去小红书同步商品===>Start");
		Calendar calendar = Calendar.getInstance();

        /* HOUR_OF_DAY 指示一天中的小时 */
		calendar.set(Calendar.HOUR_OF_DAY, calendar.get(Calendar.HOUR_OF_DAY) - 1);

		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

		String updateStart = df.format(calendar.getTime());
		String updateEnd = df.format(new Date());
		Set<String> spuIdSet = xiaohongshuService.querySpuIdByTime(updateStart, updateEnd,"CFK");
		xiaohongshuService.querySpu(spuIdSet,"cfk");
		logger.info("定时任务：自动去小红书同步商品===>End");
	}
	
	//箱包店每隔50分执行一次
	@Scheduled(cron = "0 0/50 * * * ?")
	public void runBag(){
		logger.info("定时任务：自动去小红书下载订单===>Start");
		List<Long> outOrderIdList = xiaohongshuService.xiaohongshuTradesAdd("SL");
		if(outOrderIdList.size() > 0) {
			//生成子订单并配货
			outOrderIdList.forEach(erpOrderId -> {
				outerOrderService.review(erpOrderId,"XHSL");
			});
		}
		logger.info("定时任务：自动去小红书下载订单===>End");
	}

	//箱包店每隔50分执行一次
	@Scheduled(cron = "0 0/50 * * * ?")
	public void addBagItem() throws Exception {
		logger.info("定时任务：自动去小红书同步商品===>Start");
		Calendar calendar = Calendar.getInstance();

        /* HOUR_OF_DAY 指示一天中的小时 */
		calendar.set(Calendar.HOUR_OF_DAY, calendar.get(Calendar.HOUR_OF_DAY) - 1);

		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

		String updateStart = df.format(calendar.getTime());
		String updateEnd = df.format(new Date());
		Set<String> spuIdSet = xiaohongshuService.querySpuIdByTime(updateStart, updateEnd,"SL");
		xiaohongshuService.querySpu(spuIdSet,"bag");
		logger.info("定时任务：自动去小红书同步商品===>End");
	}
	
}
